<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Tags extends CI_Controller {

	public function __construct() {
		parent::__construct();
	}

	public function index() {
		redirect();
	}
	//// this function is used to get data when click on tag /////
	public function get_data_by_tag() {
		if (!$this -> uri -> segment(3) || strlen($this -> uri -> segment(3))<12 || strlen($this -> uri -> segment(3))<12) {
			$data['error'] = "";
		}
		$data['title'] = "Post លេង";
		$data['tag'] = $this -> mod_tag -> select_tag_random();
		$data['post_record'] = $this -> mod_tag -> get_picture_tags_count();

		$this -> load -> view(F_MASTER . 'index', $data);
	}

	public function get_data_by_tag_ajax() { //get_post_by_click_tag_load
		$items_per_group = 50; //sanitize post value
		$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
		if (!is_numeric($group_number)) {
			header('HTTP/1.1 500 Invalid number!');
			exit();
		}
		$position = ($group_number * $items_per_group); //get current starting point of records
		$uri = $_POST['uri_id'];//Limit our results within a specified range.
		$results= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1,'key_tags'=>$uri),$items_per_group.",".$position,NULL,"post_id DESC");

		if ($results) {
				//output results from database
				echo '<ul class="thumbnails">';
				foreach ($results->result() as $row) {
					if($row -> count_click_views<=0){
						$views="ដង ";
					}else{
						$views="ដង ";
					}
					$str = $row -> description;
					$v_random = rand(1,2);// random class in css
					$col = "";
					if ($v_random == 1) {
						$col = "col-md-2";
					} else {
						$col = "col-md-3";
					} 
					////////// text /////////////////////////
					echo '<div class="' . $col . '" id="thumnail_over" style="margin-left: 4px; margin-right: 10px; padding-left: 0; padding-right: 0;">';
					if  ($row -> img_name == NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL ) {
						$str = substr($str, 0,150)."...";
						echo '<li class="thumbnail" id="speak">';
						//echo '<aside class="pquote">';
						
						echo '<a href="#" data-toggle="modal" onclick="popUpText(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="" row_tag_text="'."".'" data_count_click_text="' . $row -> count_click_views . '" data-target="#basicModalfortext" class="popup-modal" row_id_text="' . $row -> post_id . '" row_desc_text="' . $row -> description . '"><p style="color: #33536D;">' .'<q>&nbsp;'. $str.'&nbsp;</q>' . '</p></a>';
						
						//echo '</aside>';
						echo '<span class="caption simple-caption" style="bottom: -52px !important;">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_text'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						
						echo '<div >'.$row->embed_code.'</div>';
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					//// Upload image //////////////
					} else if($row -> img_name!=NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'." ".'" row_tag="'." ".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> img_name . '""><img src="' . $row -> img_name . '" /></a>';
						echo '<p class="text_description">'.$str.'</p><br />';
						echo '<span class="caption simple-caption">';
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span id="count_related" class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';;

					//// embed image from other url  ///////
					} else if($row -> embed_img!=NULL && $row -> embed_code==NULL && $row ->  img_name ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<p class="text_description">'.$str.'</p>';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag="'."".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> embed_img . '""><img src="' . $row -> embed_img . '" /></a>';
						
						echo '<span class="caption simple-caption">';
						
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					/////////////////// video ////////////////////////////
					} else if($row -> img_name==NULL && $row -> embed_img ==NULL && $row -> embed_code!=NULL) {
						
						$ser = array();
						$ser[] = $row -> embed_code;
						?>
						<script type="text/javascript">
							get_video();
							function get_video()
							{
								$(".oembed").oembed(null, 
								{
									embedMethod: "append", 
									maxWidth: 424,
									maxHeight: 268,
									vimeo: { autoplay: true, maxWidth: 200, maxHeight: 200}			
								});
								var array_embed_code = <?php echo json_encode($ser,JSON_UNESCAPED_SLASHES); ?>; /// convert to array javscript and escap slashes
								$("#stexpand"+<?php echo $row->post_id; ?>).oembed(array_embed_code[0]); /// validate url by concerting to video 	
							}
						</script>
						<?php
						$description = substr($row -> description, 0,30).'...';
						echo '<div onclick="emebd_video_image(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag_name_embed="'."".'" row_count_click_embed="'.$row -> count_click_views.'" row_post_id_embed="'.$row -> post_id.'" row_code_url="'.$row->embed_code.'">';
						echo '<li class="thumbnail" >';
						echo '<span id="stexpand'.$row->post_id.'">' ;

						echo '<p>'.$description.'</p>';
						echo '</span>';
						echo '<br />';
						echo '<span class="caption simple-caption">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_embed'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';
						echo '</div>';
					}
					echo '</div>';
				}
				//end of result set
			}
			echo '</ul>';/// already change
	}

	//// this function is used to get the total number of tag, ///
	///  it's action happend when we click on tag ////
	public function get_data_by_time_and_sort() {
		$data['title'] = "Post លេង";
		$data['tag'] = $this -> mod_tag -> select_tag_random();
		$uri_id_4 = $this -> uri -> segment(4);
		$nmber_sort = "";
		if (!$uri_id_4 || $uri_id_4 == "") {
				$data['error']="";
				$this -> load -> view(F_MASTER . 'index', $data);
		} else {
			if ($uri_id_4 == "one-day-ago" || $uri_id_4 == "three-days-ago" || $uri_id_4 == "one-week-ago") {
				if ($uri_id_4 == "one-day-ago") {
					$nmber_sort = 1;
				} else if ($uri_id_4 == "three-days-ago") {
					$nmber_sort = 3;
				} else if ($uri_id_4 == "one-week-ago") {
					$nmber_sort = 7;
				}
				$data['post_record'] = $this -> mod_tag -> get_picture_tag_count_by_id_and_time($nmber_sort);
				$this -> load -> view(F_MASTER . 'index', $data);

			} else {
				$data['error']="";
				$this -> load -> view(F_MASTER . 'index', $data);
			}
		}

	}
	//// this function is used to view all data when we click on tag ////
	
	public function get_data_by_time_and_sort_ajax() { //get_tag_by_time_click_on_tag_result
		$items_per_group = 50;
		$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
		if (!is_numeric($group_number)) {
			header('HTTP/1.1 500 Invalid number!');
			exit();
		}
		$position = ($group_number * $items_per_group);
		$uri_id_3 = $_POST['uri_id_3'];
		$uri_id_4 = $_POST['uri_id_4'];
		$nmber_sort = "";
		if ($uri_id_4 == "one-day-ago") {
			$nmber_sort = 1;
		} else if ($uri_id_4 == "three-days-ago") {
			$nmber_sort = 3;
		} else if ($uri_id_4 == "one-week-ago") {
			$nmber_sort = 7;
		}
		$results = $this -> mod_tag -> get_data_by_click_on_tag($uri_id_3,$nmber_sort,$items_per_group, $position);
		
		if ($results) {
				//output results from database
				echo '<ul class="thumbnails">';
				foreach ($results->result() as $row) {
					if($row -> count_click_views<=0){
						$views="ដង ";
					}else{
						$views="ដង ";
					}
					$str = $row -> description;
					$v_random = rand(1,2);// random class in css
					$col = "";
					if ($v_random == 1) {
						$col = "col-md-2";
					} else {
						$col = "col-md-3";
					} 
					////////// text /////////////////////////
					echo '<div class="' . $col . '" id="thumnail_over" style="margin-left: 4px; margin-right: 10px; padding-left: 0; padding-right: 0;">';
					if  ($row -> img_name == NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL ) {
						$str = substr($str, 0,150)."...";
						echo '<li class="thumbnail" id="speak">';
						//echo '<aside class="pquote">';
						
						echo '<a href="#" data-toggle="modal" onclick="popUpText(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="" row_tag_text="'."".'" data_count_click_text="' . $row -> count_click_views . '" data-target="#basicModalfortext" class="popup-modal" row_id_text="' . $row -> post_id . '" row_desc_text="' . $row -> description . '"><p style="color: #33536D;">' .'<q>&nbsp;'. $str.'&nbsp;</q>' . '</p></a>';
						
						//echo '</aside>';
						echo '<span class="caption simple-caption" style="bottom: -52px !important;">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_text'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						
						echo '<div >'.$row->embed_code.'</div>';
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					//// Upload image //////////////
					} else if($row -> img_name!=NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'." ".'" row_tag="'." ".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> img_name . '""><img src="' . $row -> img_name . '" /></a>';
						echo '<p class="text_description">'.$str.'</p><br />';
						echo '<span class="caption simple-caption">';
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span id="count_related" class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';;

					//// embed image from other url  ///////
					} else if($row -> embed_img!=NULL && $row -> embed_code==NULL && $row ->  img_name ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<p class="text_description">'.$str.'</p>';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag="'."".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> embed_img . '""><img src="' . $row -> embed_img . '" /></a>';
						
						echo '<span class="caption simple-caption">';
						
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					/////////////////// video ////////////////////////////
					} else if($row -> img_name==NULL && $row -> embed_img ==NULL && $row -> embed_code!=NULL) {
						
						$ser = array();
						$ser[] = $row -> embed_code;
						?>
						<script type="text/javascript">
							get_video();
							function get_video()
							{
								$(".oembed").oembed(null, 
								{
									embedMethod: "append", 
									maxWidth: 424,
									maxHeight: 268,
									vimeo: { autoplay: true, maxWidth: 200, maxHeight: 200}			
								});
								var array_embed_code = <?php echo json_encode($ser,JSON_UNESCAPED_SLASHES); ?>; /// convert to array javscript and escap slashes
								$("#stexpand"+<?php echo $row->post_id; ?>).oembed(array_embed_code[0]); /// validate url by concerting to video 	
							}
						</script>
						<?php
						$description = substr($row -> description, 0,30).'...';
						echo '<div onclick="emebd_video_image(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag_name_embed="'."".'" row_count_click_embed="'.$row -> count_click_views.'" row_post_id_embed="'.$row -> post_id.'" row_code_url="'.$row->embed_code.'">';
						echo '<li class="thumbnail" >';
						echo '<span id="stexpand'.$row->post_id.'">' ;

						echo '<p>'.$description.'</p>';
						echo '</span>';
						echo '<br />';
						echo '<span class="caption simple-caption">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_embed'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';
						echo '</div>';
					}
					echo '</div>';
				}
				//end of result set
			}
			echo '</ul>';/// already change
	}

	/// this function is used to get data when user search ////
	public function get_data_by_search() {
		$data['title'] = "Post លេង";
		$keyword = isset($_GET['tag'])?$_GET['tag']:null; ///$this -> input -> post('tag');
		if (isset($keyword)) {// search box submit
			if ($keyword == '' || $keyword == null) {
				$data['error']="";
				$data['tag'] = $this -> mod_tag -> select_tag_random();
				$this -> load -> view(F_MASTER . 'index', $data);
			} else {
				
				$data['tag'] = $this -> mod_tag -> select_tag_random();
				$data['post_record'] = $this -> mod_tag -> get_tags_by_box_count($keyword);
				$this -> session -> set_userdata(array('tag_sessoin' => $keyword));
				$this -> load -> view(F_MASTER . 'index', $data);
			}
		} else {
				$data['error']="";
				$data['tag'] = $this -> mod_tag -> select_tag_random();
				$this -> load -> view(F_MASTER . 'index', $data);
		}
	}

	public function get_data_by_search_ajax() { //get_tag_by_search_tag
		$items_per_group = 50;
		$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
		if (!is_numeric($group_number)) {
			header('HTTP/1.1 500 Invalid number!');
			exit();
		}
		$position = ($group_number * $items_per_group);
		$uri = $_POST["uri_id"];
		$results = $this -> mod_tag -> get_data_by_search($uri,$items_per_group, $position);
		
		if ($results) {
				//output results from database
				echo '<ul class="thumbnails">';
				foreach ($results->result() as $row) {
					if($row -> count_click_views<=0){
						$views="ដង ";
					}else{
						$views="ដង ";
					}
					$str = $row -> description;
					$v_random = rand(1,2);// random class in css
					$col = "";
					if ($v_random == 1) {
						$col = "col-md-2";
					} else {
						$col = "col-md-3";
					} 
					////////// text /////////////////////////
					echo '<div class="' . $col . '" id="thumnail_over" style="margin-left: 4px; margin-right: 10px; padding-left: 0; padding-right: 0;">';
					if  ($row -> img_name == NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL ) {
						$str = substr($str, 0,150)."...";
						echo '<li class="thumbnail" id="speak">';
						//echo '<aside class="pquote">';
						
						echo '<a href="#" data-toggle="modal" onclick="popUpText(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="" row_tag_text="'."".'" data_count_click_text="' . $row -> count_click_views . '" data-target="#basicModalfortext" class="popup-modal" row_id_text="' . $row -> post_id . '" row_desc_text="' . $row -> description . '"><p style="color: #33536D;">' .'<q>&nbsp;'. $str.'&nbsp;</q>' . '</p></a>';
						
						//echo '</aside>';
						echo '<span class="caption simple-caption" style="bottom: -52px !important;">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_text'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						
						echo '<div >'.$row->embed_code.'</div>';
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					//// Upload image //////////////
					} else if($row -> img_name!=NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'." ".'" row_tag="'." ".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> img_name . '""><img src="' . $row -> img_name . '" /></a>';
						echo '<p class="text_description">'.$str.'</p><br />';
						echo '<span class="caption simple-caption">';
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span id="count_related" class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';;

					//// embed image from other url  ///////
					} else if($row -> embed_img!=NULL && $row -> embed_code==NULL && $row ->  img_name ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<p class="text_description">'.$str.'</p>';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag="'."".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> embed_img . '""><img src="' . $row -> embed_img . '" /></a>';
						
						echo '<span class="caption simple-caption">';
						
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					/////////////////// video ////////////////////////////
					} else if($row -> img_name==NULL && $row -> embed_img ==NULL && $row -> embed_code!=NULL) {
						
						$ser = array();
						$ser[] = $row -> embed_code;
						?>
						<script type="text/javascript">
							get_video();
							function get_video()
							{
								$(".oembed").oembed(null, 
								{
									embedMethod: "append", 
									maxWidth: 424,
									maxHeight: 268,
									vimeo: { autoplay: true, maxWidth: 200, maxHeight: 200}			
								});
								var array_embed_code = <?php echo json_encode($ser,JSON_UNESCAPED_SLASHES); ?>; /// convert to array javscript and escap slashes
								$("#stexpand"+<?php echo $row->post_id; ?>).oembed(array_embed_code[0]); /// validate url by concerting to video 	
							}
						</script>
						<?php
						$description = substr($row -> description, 0,30).'...';
						echo '<div onclick="emebd_video_image(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag_name_embed="'."".'" row_count_click_embed="'.$row -> count_click_views.'" row_post_id_embed="'.$row -> post_id.'" row_code_url="'.$row->embed_code.'">';
						echo '<li class="thumbnail" >';
						echo '<span id="stexpand'.$row->post_id.'">' ;

						echo '<p>'.$description.'</p>';
						echo '</span>';
						echo '<br />';
						echo '<span class="caption simple-caption">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_embed'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';
						echo '</div>';
					}
					echo '</div>';
				}
				//end of result set
			}
			echo '</ul>'; /// already change
	}

	/// this function is used to complete key word serach ///
	/// it is ajax search ////
	public function auto_complete() {
		$keyword = $this -> input -> post('q');
		$query = $this -> mod_tag -> auto_complete_search($keyword);
		foreach ($query as $row) {
			echo $row -> name . "\n";
		}
	}

	//// this function is used to get data by sorting  ////
	///  you will see when stay on the first page of postleng ///
	public function get_data_by_sort() {
		$uri = $this -> uri -> segment(3);
		$data['title'] = "Post លេង";
		if (!$uri || $uri == "") {
			$data['error'] = "no_record";
			$data['tag'] = $this -> mod_tag -> select_tag_random();
			$this -> load -> view(F_MASTER . 'index', $data);
		} else {
			if ($uri == "one-day-ago" || $uri == "three-days-ago" || $uri == "one-week-ago") {
				if ($uri == "one-day-ago") {
					$nmber_sort = 1;
				} else if ($uri == "three-days-ago") {
					$nmber_sort = 3;
				} else if ($uri == "one-week-ago") {
					$nmber_sort = 7;
				}
				$data['post_record'] = $this -> mod_tag -> get_tags_sort_by_time_count($nmber_sort);
				$data['tag'] = $this -> mod_tag -> select_tag_random();
				$this -> load -> view(F_MASTER . 'index', $data);
			} else {
				$data['error'] = "no_record";
				$data['tag'] = $this -> mod_tag -> select_tag_random();
				$this -> load -> view(F_MASTER . 'index', $data);
			}
		}
	}

	///// view record by time /////
	public function get_data_by_sort_ajax() { ///get_record_by_time
		$items_per_group = 50;
		$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
		if (!is_numeric($group_number)) {
			header('HTTP/1.1 500 Invalid number!');
			exit();
		}
		$position = ($group_number * $items_per_group);
		$uri = intval($_POST["uri_id"]);
		$results= $this -> mod_tag -> get_data_by_time($uri,$items_per_group, $position);
		
		if ($results) {
				//output results from database
				echo '<ul class="thumbnails">';
				foreach ($results->result() as $row) {
					if($row -> count_click_views<=0){
						$views="ដង ";
					}else{
						$views="ដង ";
					}
					$str = $row -> description;
					$v_random = rand(1,2);// random class in css
					$col = "";
					if ($v_random == 1) {
						$col = "col-md-2";
					} else {
						$col = "col-md-3";
					} 
					////////// text /////////////////////////
					echo '<div class="' . $col . '" id="thumnail_over" style="margin-left: 4px; margin-right: 10px; padding-left: 0; padding-right: 0;">';
					if  ($row -> img_name == NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL ) {
						$str = substr($str, 0,150)."...";
						echo '<li class="thumbnail" id="speak">';
						//echo '<aside class="pquote">';
						
						echo '<a href="#" data-toggle="modal" onclick="popUpText(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="" row_tag_text="'."".'" data_count_click_text="' . $row -> count_click_views . '" data-target="#basicModalfortext" class="popup-modal" row_id_text="' . $row -> post_id . '" row_desc_text="' . $row -> description . '"><p style="color: #33536D;">' .'<q>&nbsp;'. $str.'&nbsp;</q>' . '</p></a>';
						
						//echo '</aside>';
						echo '<span class="caption simple-caption" style="bottom: -52px !important;">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_text'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						
						echo '<div >'.$row->embed_code.'</div>';
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					//// Upload image //////////////
					} else if($row -> img_name!=NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'." ".'" row_tag="'." ".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> img_name . '""><img src="' . $row -> img_name . '" /></a>';
						echo '<p class="text_description">'.$str.'</p><br />';
						echo '<span class="caption simple-caption">';
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span id="count_related" class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';;

					//// embed image from other url  ///////
					} else if($row -> embed_img!=NULL && $row -> embed_code==NULL && $row ->  img_name ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<p class="text_description">'.$str.'</p>';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag="'."".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> embed_img . '""><img src="' . $row -> embed_img . '" /></a>';
						
						echo '<span class="caption simple-caption">';
						
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					/////////////////// video ////////////////////////////
					} else if($row -> img_name==NULL && $row -> embed_img ==NULL && $row -> embed_code!=NULL) {
						
						$ser = array();
						$ser[] = $row -> embed_code;
						?>
						<script type="text/javascript">
							get_video();
							function get_video()
							{
								$(".oembed").oembed(null, 
								{
									embedMethod: "append", 
									maxWidth: 424,
									maxHeight: 268,
									vimeo: { autoplay: true, maxWidth: 200, maxHeight: 200}			
								});
								var array_embed_code = <?php echo json_encode($ser,JSON_UNESCAPED_SLASHES); ?>; /// convert to array javscript and escap slashes
								$("#stexpand"+<?php echo $row->post_id; ?>).oembed(array_embed_code[0]); /// validate url by concerting to video 	
							}
						</script>
						<?php
						$description = substr($row -> description, 0,30).'...';
						echo '<div onclick="emebd_video_image(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag_name_embed="'."".'" row_count_click_embed="'.$row -> count_click_views.'" row_post_id_embed="'.$row -> post_id.'" row_code_url="'.$row->embed_code.'">';
						echo '<li class="thumbnail" >';
						echo '<span id="stexpand'.$row->post_id.'">' ;

						echo '<p>'.$description.'</p>';
						echo '</span>';
						echo '<br />';
						echo '<span class="caption simple-caption">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_embed'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';
						echo '</div>';
					}
					echo '</div>';
				}
				//end of result set
			}
			echo '</ul>'; /// already change
	}

	/// this function is used to get data when user search,////
	/// and they want to sort the time ////
	public function get_data_by_search_and_sort() {

		$data['title'] = "Post លេង";
		$tag = isset($_GET['tag'])?$_GET['tag']:null;////$this -> session -> userdata('tag_sessoin');
		$uri_id_4 = isset($_GET['time'])?$_GET['time']:null;
		$nmber_sort = "";
		if (!$uri_id_4 || $uri_id_4 == "" || !$tag || $tag=="") {
			$data['error'] = "no_record";
			$data['tag'] = $this -> mod_tag -> select_tag_random();
			$this -> load -> view(F_MASTER . 'index', $data);
		} else {
			if ($uri_id_4 == "one-day-ago" || $uri_id_4 == "three-days-ago" || $uri_id_4 == "one-week-ago") {
				if ($uri_id_4 == "one-day-ago") {
					$nmber_sort = 1;
				} else if ($uri_id_4 == "three-days-ago") {
					$nmber_sort = 3;
				} else if ($uri_id_4 == "one-week-ago") {
					$nmber_sort = 7;
				}
				$data['post_record'] = $this -> mod_tag -> get_tag_sort_by_time_tag_count($tag, $nmber_sort);
				$data['tag'] = $this -> mod_tag -> select_tag_random();
				$this -> load -> view(F_MASTER . 'index', $data);
			} else {
				$data['error'] = "no_record";
				$data['tag'] = $this -> mod_tag -> select_tag_random();
				$this -> load -> view(F_MASTER . 'index', $data);
			}
		}
	}
	//// this function is used to get data when user search ////
	//// and use functoin sort by time ////
	public function get_data_by_search_and_sort_ajax() { //get_tag_sort_by_tag_time_load
		$items_per_group = 20;
		$group_number = filter_var($_POST["group_no"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
		if (!is_numeric($group_number)) {
			header('HTTP/1.1 500 Invalid number!');
			exit();
		}
		$position = ($group_number * $items_per_group);
		$uri = intval($_POST["uri_id"]);
		$tag = $_POST["uri_tag"];//$this -> session -> userdata('tag_sessoin');
		$results = $this -> mod_tag ->  get_data_by_search_and_sort($tag,$uri,$items_per_group, $position);
		
		if ($results) {
				//output results from database
				echo '<ul class="thumbnails">';
				foreach ($results->result() as $row) {
					if($row -> count_click_views<=0){
						$views="ដង ";
					}else{
						$views="ដង ";
					}
					$str = $row -> description;
					$v_random = rand(1,2);// random class in css
					$col = "";
					if ($v_random == 1) {
						$col = "col-md-2";
					} else {
						$col = "col-md-3";
					} 
					////////// text /////////////////////////
					echo '<div class="' . $col . '" id="thumnail_over" style="margin-left: 4px; margin-right: 10px; padding-left: 0; padding-right: 0;">';
					if  ($row -> img_name == NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL ) {
						$str = substr($str, 0,150)."...";
						echo '<li class="thumbnail" id="speak">';
						//echo '<aside class="pquote">';
						
						echo '<a href="#" data-toggle="modal" onclick="popUpText(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="" row_tag_text="'."".'" data_count_click_text="' . $row -> count_click_views . '" data-target="#basicModalfortext" class="popup-modal" row_id_text="' . $row -> post_id . '" row_desc_text="' . $row -> description . '"><p style="color: #33536D;">' .'<q>&nbsp;'. $str.'&nbsp;</q>' . '</p></a>';
						
						//echo '</aside>';
						echo '<span class="caption simple-caption" style="bottom: -52px !important;">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_text'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						
						echo '<div >'.$row->embed_code.'</div>';
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					//// Upload image //////////////
					} else if($row -> img_name!=NULL && $row -> embed_code==NULL && $row -> embed_img ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'." ".'" row_tag="'." ".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> img_name . '""><img src="' . $row -> img_name . '" /></a>';
						echo '<p class="text_description">'.$str.'</p><br />';
						echo '<span class="caption simple-caption">';
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span id="count_related" class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';;

					//// embed image from other url  ///////
					} else if($row -> embed_img!=NULL && $row -> embed_code==NULL && $row ->  img_name ==NULL) {
						if (strlen($str) > 30 && strlen($str) < 101) {
							$str = substr($str, 0, 30) . '...';
						}
						echo '<li class="thumbnail" >';
						echo '<p class="text_description">'.$str.'</p>';
						echo '<a href="#" class="tag_name_herf" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag="'."".'" data-toggle="modal" onclick="popUpDetail(this)" data_count_click="' . $row -> count_click_views . '" data-target="#basicModal" class="popup-modal" 
						row_id="' . $row -> post_id . '" tag_id="'.$row->tag_id.'" row_des="' . $row -> description . '" row_img="' . $row -> embed_img . '""><img src="' . $row -> embed_img . '" /></a>';
						
						echo '<span class="caption simple-caption">';
						
						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';

					/////////////////// video ////////////////////////////
					} else if($row -> img_name==NULL && $row -> embed_img ==NULL && $row -> embed_code!=NULL) {
						
						$ser = array();
						$ser[] = $row -> embed_code;
						?>
						<script type="text/javascript">
							get_video();
							function get_video()
							{
								$(".oembed").oembed(null, 
								{
									embedMethod: "append", 
									maxWidth: 424,
									maxHeight: 268,
									vimeo: { autoplay: true, maxWidth: 200, maxHeight: 200}			
								});
								var array_embed_code = <?php echo json_encode($ser,JSON_UNESCAPED_SLASHES); ?>; /// convert to array javscript and escap slashes
								$("#stexpand"+<?php echo $row->post_id; ?>).oembed(array_embed_code[0]); /// validate url by concerting to video 	
							}
						</script>
						<?php
						$description = substr($row -> description, 0,30).'...';
						echo '<div onclick="emebd_video_image(this)" row_baseUrl="'.base_url().'tags/get-data-by-tag/'.'" row_key_code="'."".'" row_tag_name_embed="'."".'" row_count_click_embed="'.$row -> count_click_views.'" row_post_id_embed="'.$row -> post_id.'" row_code_url="'.$row->embed_code.'">';
						echo '<li class="thumbnail" >';
						echo '<span id="stexpand'.$row->post_id.'">' ;

						echo '<p>'.$description.'</p>';
						echo '</span>';
						echo '<br />';
						echo '<span class="caption simple-caption">';

						echo '<p class="bgdescolor">';
						echo '<span class="text-icons"><i class="glyphicon glyphicon-eye-open"></i>បង្ហាញ​'.'<span class="counting_view_embed'.$row -> post_id.'">'.' '.$row->count_click_views.'</span>'.' '.'<span>'.$views.'|<span> ';
						$querys= $this ->mod_globals-> select_join(PREFIX_TABLE.'post_has_tags', array(PREFIX_TABLE.'posts' => array('use_post_id' => 'post_id'),PREFIX_TABLE.'tags'=> array('use_tag_id'=>'tag_id')),'inner',array(PREFIX_TABLE.'posts.status' => 1, 'use_post_id'=>$row -> post_id));
						foreach ($querys->result() as $key) {
							echo '<a class="badge" style="background: #fff !important; color: #728EA4;" href="'.base_url()."tags/get-data-by-tag/".$key->key_tags.'">'.$key->name.'</a>';
						}
						echo '</p>';
						echo '</span>';
						echo '</a>';
						echo '</li>';
						echo '</div>';
					}
					echo '</div>';
				}
				//end of result set
			}
			echo '</ul>'; /// already change
	//// Create new tags ///
}
	////////////////// Popular tag ///////////////
	public function get_popular_tag(){
		$data['title'] = "Post លេង";
		$data['popoular_tag'] = $this->mod_tag->get_popular_tag();
		$data['tag'] = $this -> mod_tag -> select_tag_random();
		$this -> load -> view(F_MASTER . 'index', $data);
	}
	///////// Add new Tag using Jquery Ajax no reload page
	public function new_tags(){	

		$new_tag_name = $this->input->post('new_name');

		if($this->mod_globals->check_data_exist(PREFIX_TABLE."tags",array("name"=>$new_tag_name))){
				echo "exits";
		}else{
			$key_tags =  random_string('alnum', 12);
		$last_tag_id=$this->mod_globals->insert_get_last_id(PREFIX_TABLE."tags", array('name' => $new_tag_name,'key_tags'=>$key_tags));

		//$tag_last = $this -> db -> query('SELECT tag_id, name as tag_name FROM '.PREFIX_TABLE.'tags where tag_id = (select max(tag_id) from '.PREFIX_TABLE.'tags)');
		$all_tag=$this->mod_globals->select_all(PREFIX_TABLE."tags");
		?>
<?php
		foreach ($all_tag->result() as $row) {
			$tag_id=$row->tag_id;
			$tag_name=$row->name;

			if($last_tag_id==$tag_id){
?>
				<option id-tag"<?php echo $tag_id;?>" selected="selected"><?php echo $tag_name;?></option>
<?php
			}else{
?>
				<option id-tag"<?php echo $tag_id;?>"><?php echo $tag_name;?></option>
<?php
			}
		}
		?>

		<script type="text/javascript">
		document.getElementById("storeidtag").value = <?php echo $tag_id; ?>;
		</script>
		<?php

}
		}
		
	}

